(function($) {
	$.fn.combox = function(options) {
		var opts = $.extend({}, $.fn.combox.defaults, options);
		if (typeof(opts.callBack)!=='function')
			opts.callBack = function(){};

		return this.each(function() {
			var self = $(this);
			if(self.children().length == 0) return false;
			$(this).css("display","block");
			$(this).css("visibility","hidden");
			var j = self[0].selectedIndex;

			if(self.css("width") == "auto")
				var w = opts.sel_min_width;
			else if(parseInt(self.css("width")) != 0)
				var w = self.css("width");
			else
				var w =  opts.sel_min_width;
			
			if(self.css("height") == "auto")
				var h = opts.li_lineheight;
			else if(parseInt(self.css("height")) != 0)
				var h = self.css("height");
			else
				var h =  opts.li_lineheight;

			var pid = opts.prefix_id+self.attr("name");
			$('<div id="'+pid+'">'+self.children().eq(j).text()+'</div>').css({
				"width":w,
				"height":"19px",
				"top":self.offset().top,
				"left":self.offset().left,
				"background":"#fff url(down.gif) no-repeat right -1px",
				"border":"1px solid #bbb",
				"lineHeight":"19px",
				"textIndent":"5px",
				"overflow":"hidden",
				"padding-right":"20px",
				"cursor":"pointer",
				"_cursor":"hand"
			}).appendTo(self.parent()).show();
			
			$("#"+pid).click(function(e){
				var cid = "child_"+self.attr("name");
				$(this).css({"z-index":opts.over_div_index});
				if($.fn.combox.curr_id != 'id' && cid != $.fn.combox.curr_id){
					$("#"+$.fn.combox.curr_id).hide();
				}
				$.fn.combox.curr_id = cid;
				if($("#"+cid).length > 0){
					$("#"+cid).show();
					$("#"+cid).children().show();
				}else{
					var w = parseInt($("#"+pid).css("width")) + 20 + "px";
					$('<div id="'+cid+'"></div>').css({
						"position":"absolute",
						"width":w,
						"background-color":opts.div_bg_color,
						"border":"1px solid #bbb",
						"border-top":"0px",
						"z-index":opts.over_div_index + 1
					}).hover(function(){
							$(this).show()
						},function(){
							$(this).hide();
							e.stopPropagation();
					}).appendTo(self.parent());
					
					$('<ul id="ul_'+cid+'"></ul>').css({
						"listStyle":"none",
						"margin":0,
						"padding":0,
						"height":h,
						"hite-space":"nowrap",
						"overflow":"hidden",
						"overflow-y":"auto"
					}).appendTo($("#"+cid));

					self.children().each(function(i){
						var opt = $(this);
						var bgcolor = $(this).css("background");
						$("<li>"+$(this).text()+"</li>").css({
							"height":"20px",
							"lineHeight":"20px",
							"textIndent":"5px",
							'background':bgcolor
						}).hover(function(){
							$(this).css({"background-color":opts.over_bg_color,"color":"#fff","cursor":"default"});
						},function(){
							if(bgcolor === undefined || bgcolor == '') $(this).css({"background-color":"#FFFFFF","color":"#000"});
							else $(this).css({"background-color":"#d6d6d6","color":"#000"});
							e.stopPropagation();
						}).click(function(){
							$("#"+pid).html($(this).text());
							self[0].selectedIndex = i;
							$("#"+cid).hide();
							opts.callBack(self.attr("name"),$(self[0]).val());
						}).appendTo($("#ul_"+cid));
					});
				}
			});

			$(this).css("display","none");
		});
	};

	$.fn.combox.defaults = {
		prefix_id:"div_",
		sel_min_width:"150px",
		div_bg_color:"#fff",
		over_bg_color:"#787878",
		over_div_index:10,
		font_size:"12px",
		li_lineheight:"160px",
		curr_id:"",
		callBack: function(){}
	};

	$.fn.combox.curr_id = 'id';
})(jQuery);